package org.noootsab.gae.games.tzaar.model.game {
import com.adobe.serialization.json.JSON;

public class GameAction {
    public static const STACK_ON:uint = 0;
    public static const TAKE:uint = 1;

    private var action:uint;
    private var active:Piece;
    private var passive:Piece;
    private var from:Array;
    private var to:Array;

    public function GameAction(action:uint, active:Piece, from:Array, passive:Piece, to:Array) {
        this.action = action;

        this.active = active;
        this.from = from; //[line, cell]

        this.passive = passive;
        this.to = to; //[line, cell]
    }

    public static function fromJSON(game:Game, json:*):GameAction {
        var jsonAction:Object;
        if (json is String) {
            jsonAction = JSON.decode(json) as Object;
        } else if (json is Object) {
            jsonAction = json;
        } else {
            throw new Error("Bad Json given");
        }

        //todo pieces...
        var gameAction:GameAction = new GameAction(jsonAction.action, null, jsonAction.hasOwnProperty("from")?jsonAction.from:null, null, jsonAction.hasOwnProperty("to")?jsonAction.to:null);

        return gameAction;
    }

    public static function toJSON(gameAction:GameAction):String {
        var json:String = "{";

        json += "\"action\":" + gameAction.action;

        //todo pieces...

        if (gameAction.from !== null) {
            json += ",";
            json += "\"from\":" + "[" + gameAction.from[0] + "," + gameAction.from[1] + "]";
        }

        if (gameAction.to !== null) {
            json += ",";
            json += "\"to\":" + "[" + gameAction.to[0] + "," + gameAction.to[1] + "]";
        }

        json += "}";
        return json;
    }
}
}